<?php
/**
 * @copyright
 * @author :
 * @version :
 * @name :
 * @todo:
*/
class VimuaMenu extends CModule {
	var $category_id;
	/**
	 * Init code for module
	 *
	 */
	function init(){		
		$this->html_code = $this->ul_li(CStoreCategoryList::getInstance()->getCategories());
	}

	/**
	 * Name for cache
	 *
	 */
	function getCacheName() {
		if (!isset($this->category_id)) {
			if(!($this->category_id = request('category_id'))){
				if(request('page') == 'product_detail' and request('id') and
					$product = CTable('product')->fields('category_id')->select(request('id'))){
					$this->category_id = $product['category_id'];
				} else $this->category_id='laptop';
			}
		}
		return parent::getCacheName().CStoreCategoryList::getInstance()->getRootCategory($this->category_id);
	}

	/**
	 * Draw function
	 *
	 */
	function draw(){
		CForm($this->path.'/layout.tpl')->parse(array(
			'content'=>output()->get($this->on_redraw()),
			'category' => $this->category_id,
		));
	}

	/**
	 * Redraw function for ajax call
	 *
	 */
	function on_redraw(){
		CForm($this->path().'/detail.tpl')->parse(
			array(
				'ul_li'=>$this->html_code,
			)
		);
 	}

 	/**
 	 * Generate html code
 	 *
 	 * @param unknown_type $categories
 	 * @param unknown_type $level
 	 * @return unknown
 	 */
 	function ul_li($categories, $level = 1){
		$st = '';
		$skin = portal()->skin();
		foreach($categories as &$category){
			$ul_li = '';
			if(isset($category['cats']) and $category['cats']){				
				$ul_li = $this->ul_li($category['cats'], $level+1); // Menu cap 2
			}
			
			if(isset($category['id'])){
				if($category['is_homepage']){
					if(!isset($category['url'])){
						$category['url'] = url('product_list',array('category_id'=>$category['id'], 'portal'=>'vimua'));
					}
					//blacknight's
					if(!isset($category['direct_url']) || $category['direct_url'] == NULL){
						$category['direct_url'] = url('product_list',array('category_id'=>$category['id'], 'portal'=>'vimua'), '', '1');
					}
					if(isset($category['cats']) and $category['cats'] and $ul_li){ // Menu cap 1
//						$st .= '<div class="menu_con">
//									<div class="menucon_muiten">
//										<img src="/'.$skin.'/images/icon/ten_menuleftsite.gif" height="5" width="3" />&nbsp;<a href="'.$category['url'].' return false;" class="text_bigdart" id="menuitem_'.$category['id'].'">'.$category['name'].'</a>
//									</div>';
						//blacknight's
						$st .= '<div class="menu_con">
									<div class="menucon_muiten">
										<img src="/'.$skin.'/images/icon/ten_menuleftsite.gif" height="5" width="3" />&nbsp;<a href="'.$category['direct_url'].'" onClick="'.$category['url'].' return false;" class="text_bigdart" id="menuitem_'.$category['id'].'">'.$category['name'].'</a>
									</div>';
						$st .= '<div class="menu_trong"><ul>'.$ul_li.'</ul></div>'; // Noi them chuoi gen tu menu cap 2
						$st .= '</div>';
					}else{
						$st .= '<li><img src="/'.$skin.'/images/icon/ten_leftmenu.gif" height="9" width="5" />&nbsp;';
//						$st .= ($category['url']?'<a id="menuitem_'.$category['id'].'" href="'.$category['url'].'" class="text_path">':'').$category['name'].($category['url']?'</a>':'');

						//blacknight 's
						$st .= ($category['url']?'<a id="menuitem_'.$category['id'].'" href="'.$category['direct_url'].'" onClick="'.$category['url'].' return false;" class="text_path">':'').$category['name'].($category['url']?'</a>':'');
						$st .= '</li>';
					}
				}
			}
		}
		return $st;
 	}
}
?>